Page({
  data: {
    kind:'2',
    // groups: [{
    //    name: '哈哈哈小组', 
    //    members:  ['hh','haha'] ,
    //    captain: '嘻嘻嘻嘻',
    // },{
    //     name: 'st', 
    //     members: 'tr',
    //     captain: 'tr',
    // }],
    searchValue: '',
    showClearBtn: false,
    exam_id: "",
    scroll: "_1",

    groups_original: [],
    groups: [],
    level_arr: [],

    images: [],

    markType: 1,

    selectType: 0,

    hidden: [],

    buttonHint: [],

    scores: [],

    submit_btn: 1,
    index_hint:0
  },

  //输入内容时
  searchActiveChangeinput: function(e) {
    var that = this
    const val = e.detail.value;
    if (isNaN(parseInt(val))) {
      that.setData({
        searchValue: '',
        showClearBtn: false,
        scroll: "_1"
      })
    } else {
      var index = parseInt(val)
      if (index >= 1 && index <= that.data.groups.length) {
        that.setData({
          showClearBtn: true,
          scroll: "_" + index
        })
      } else if (index < 1) {
        that.setData({
          showClearBtn: true,
          scroll: "_1"
        })
      } else if (index > that.data.groups.length) {
        that.setData({
          showClearBtn: true,
          scroll: "_" + that.data.groups.length
        })
      }
    }
  },

  //点击清除搜索内容
  searchActiveChangeclear: function(e) {
    this.setData({
      showClearBtn: false,
      searchValue: ''
    })
  },

  generateImages: function(num = 12) {
    const image_list = ["/images/shuta.png", "/images/hema.png", "/images/huanxiong.png", "/images/haiyaoyu.png", "/images/houzi.png", "/images/baitu.png", "/images/yanjingshe.png", "/images/she.png", "/images/hudie.png", "/images/pangxie.png", "/images/daishu.png", "/images/luotuo.png", "/images/tuoniao.png", "/images/hashiqi.png"]

    var images_temp = []
    for (var i = 0; i < num; i++) {
      var picture = Math.floor(Math.random() * 14)
      images_temp.push(image_list[picture])
    }
    console.log(images_temp)
    this.setData({
      images: images_temp
    })
  },

  onShow: function() {
    var that = this
    //获取已签到小组的信息
    wx.request({
      url: getApp().globalData.my_server + '/mark/get_all_groups',
      data: {
        exam_id: that.data.exam_id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          that.setData({
            groups_original: res.data.groups
          })

          var hidden_first = [];
          var button_h = [];

          var level_arr_temp = that.data.level_arr
          for (var k = 0; k < res.data.groups.length; k++) {
            if (res.data.groups[k].is_marked == "1") {
              level_arr_temp[k] = 1,
                hidden_first[k] = 1,
                button_h[k] = "已提交"
            } else if (res.data.groups[k].is_marked == "0") {
              level_arr_temp[k] = 0,
                hidden_first[k] = 0,
                button_h[k] = "提交"
            }
          }

          that.setData({
            level_arr: level_arr_temp,
            hidden: hidden_first,
            buttonHint: button_h
          });


          var i = 0
          var groupLength = res.data.groups.length

          that.generateImages(groupLength)

          //获取小组成员名字信息
          var member = []
          var member1 = []
          var member2 = []

          console.log(res.data.groups.length)
          while (i < groupLength) {
            member[i] = res.data.groups[i].members
            member1[i] = member[i].split("/")
            i++
          }

          console.log(member1)

          var j = 0
          var memberDetail = []

          while (j < groupLength) {

            var k = 0
            var memberNum = member1[j].length

            memberDetail.push([])

            while (k < memberNum) {

              memberDetail[j].push(member1[j][k].split(":"))
              k++

            }
            j++


          }
          console.log(memberDetail)




          //设置整组成员信息

          var group = []
          var k = 0

          //console.log(memberD[k][0][1])

          while (k < groupLength) {

            var memberName = []

            for (i = 0; i < memberDetail[k].length; i++) {
              memberName.push(memberDetail[k][i][1])

            }


            var mid = {
              name: res.data.groups[k].name,
              members: memberName
            }
            group.push(mid);


            k++

          }

          console.log(group)

          that.setData({
            groups: group
          })

        }

        for(var h = 0; h < that.data.groups_original.length; h++){
          var flag = that.data.groups_original[h].members.split("/").map((person) => {
            return person.split(":")[0]
          }).includes(getApp().globalData.user.studentNo)

          if(flag){
            that.setData({
              index_hint: h+1
            })
          }
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '获取已签到小组的信息失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }

    })

  },

  onLoad: function(options) {
    var that = this;

    this.setData({
      kind: getApp().globalData.kind
    })

    if (options.student != "dui") {
      var markType_temp = 1
      if (options.markType == "level") {
        markType_temp = 0
        var selectType_temp = parseInt(options.selectType)
        var level_arr = new Array(selectType_temp).fill(0);
        that.setData({
          level_arr: level_arr,
          selectType: selectType_temp,
        })
      }

      that.setData({
        exam_id: options.exam_id,
        markType: markType_temp
      })
    }

    if (options.student == "dui") {
      that.setData({
        submit_btn: 0,
        exam_id: options.exam_id,
      })
    }
  },

  formSubmit: function(e) {

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    console.log(e)
    var that = this
    var group_index = e.detail.target.dataset.id
    wx.showLoading({
      title: '正在提交',
    })
    var level_arr_temp1 = that.data.level_arr
    level_arr_temp1[group_index] = 1
    that.setData({
      level_arr: level_arr_temp1
    })


    var group_member = that.data.groups_original[group_index].members.split("/").map((person) => {
      return person.split(":")[0]
    })

    var grades_only = []
    for (var mark in e.detail.value) {
      grades_only.push(e.detail.value[mark])
    }

    var grades_temp = ""
    for (var i = 0; i < group_member.length; i++) {
      grades_temp += group_member[i] + ":" + grades_only[i]

      if (i != group_member.length - 1) {
        grades_temp += "/"
      }
    }

    //老师打分
    wx.request({
      url: getApp().globalData.my_server + '/mark/grade',
      data: {
        exam_id: that.data.exam_id,
        teacherNo: getApp().globalData.user.teacherNo,
        teacher: getApp().globalData.user.name,
        grades: grades_temp,
        group_id: that.data.groups_original[group_index]._id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)

        if (res.data.error_no == 2) {
          wx.hideLoading()
          var level_arr_temp2 = that.data.level_arr
          level_arr_temp2[group_index] = 0
          that.setData({
            level_arr: level_arr_temp2
          })
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          wx.hideLoading()

          var ifSucceed = true;

          var hidden_second = that.data.hidden;

          var button_h = that.data.buttonHint;

          if (ifSucceed == true) {
            //成功
            wx.showToast({
              title: '成绩提交成功',
              icon: 'success',
              duration: 2000
            })

          }
          var submitID = e.detail.target.dataset.id
          console.log(e.detail.target.dataset);

          hidden_second[submitID] = true;
          that.setData({
            hidden: hidden_second
          });

          button_h[submitID] = "已提交";

          var level_arr_temp = that.data.level_arr
          level_arr_temp[submitID] = 1
          that.setData({
            buttonHint: button_h,
            level_arr: level_arr_temp
          });

        }
      },
      fail: function(res) {
        wx.hideLoading()
        var level_arr_temp3 = that.data.level_arr
        level_arr_temp3[group_index] = 0
        that.setData({
          level_arr: level_arr_temp3
        })
        wx.showModal({
          title: '哎呀～',
          content: '老师打分失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }

    })
  },

})